import pandas as pd
import sudachipy
#pip install sudachidict_core
tokenizer = sudachipy.Dictionary().create()
df = pd.read_excel('标日初级单词表.xlsx')
print(df.columns)
word2mapping  ={}
for ix,row in df.iterrows():
    row = dict(row)
    del row['Unnamed: 0']
    word2mapping[row['日文']] = row
    word2mapping[row['假名']] = row
all_words = list(word2mapping.keys())
match_res= []
song = '和田光司 - Butter-Fly.lrc'
with open('和田光司 - Butter-Fly.lrc',encoding='gbk', errors='replace') as reader:
    #print(reader.read())
    for lin  in reader.readlines():
        if ']' in lin:
            old = lin

            lin = lin[lin.index(']')+1:]
            tokens = tokenizer.tokenize(lin)
            texts = [t.surface() for t in tokens]


            words_hit = [e for e in all_words if e in texts]
            if words_hit:
                print('----',old,words_hit)
                for w in words_hit:
                    print(word2mapping[w])
                    match_res.append(word2mapping[w])


import collections
stat = collections.Counter([d['课文'] for d in match_res])
print(song,'匹配到单词')
from collections import defaultdict
chat2words = defaultdict(list)
for d in match_res:
    chat2words[d['课文']].append(d['日文'])
ks = sorted(stat.keys())
n = 0
for k in ks:
    v = stat[k]
    n+=len(set(chat2words[k]))
    print(k, len(set(chat2words[k])), '个',set(chat2words[k]))

#print(stat)